package com.example.lms.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lms.entity.Customers;
import com.example.lms.mapper.CustomersMapper;
import com.example.lms.mapper.ShipmentRecordsMapper;
import com.example.lms.service.CustomersService;
import com.example.lms.vo.CustomersGoodes;
import com.example.lms.vo.CustomersGoodsCountInfo;
import com.example.lms.vo.D;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.UUID;

/**
 * @module
 * @author：DUOLUONIANDAI
 * @DATA：2024/05/11
 * @Title：
 */

@Service
public class CustomersServiceImp extends ServiceImpl<CustomersMapper, Customers> implements CustomersService {
    @Override
    public ArrayList<CustomersGoodsCountInfo> getAllCustomers() {
        ArrayList<CustomersGoodsCountInfo> countCustomersInfo = super.baseMapper.getCountCustomersInfo();
        System.out.println("**************************************** 用户含有的包裹数量 ************************");
        for (CustomersGoodsCountInfo c : countCustomersInfo) {
            System.out.printf("   用户id：%5s 用户名：%4s  用户联系方式：%-15s 用户包裹数量：%-8d 用户住址：%-20s \n", c.getId(), c.getName(), c.getContactInfo(), c.getCount(), c.getAddress());
//            System.out.println(c.toString());
        }
        return countCustomersInfo;
    }

    @Override
    public D changeUserInfo(Customers customers) {

//        查询用户是否存在
        Customers id = getOne(new QueryWrapper<Customers>().eq("id", customers.getId()));
        if (ObjectUtils.isEmpty(id)) {
            D build = D.builder().build();
            build.inputVal("用户不存在", customers);
            return build;
        }

//        防止替换掉不要替换的
        if (ObjectUtils.isEmpty(customers.getName())) {
            customers.setName(id.getName());
        }
        if (ObjectUtils.isEmpty(customers.getAddress())) {
            customers.setAddress(id.getAddress());
        }
        if (ObjectUtils.isEmpty(customers.getContactInfo())) {
            customers.setContactInfo(id.getContactInfo());
        }

//        根据id进行更新
        boolean b = updateById(customers);
        Customers id1 = getOne(new QueryWrapper<Customers>().eq("id", customers.getId()));

        D build = D.builder().build();
        build.inputVal("旧的信息", id);
        build.inputVal("新的的信息", id1);
        return build;

    }

    @Autowired
    ShipmentRecordsMapper shipmentRecordsMapper;

    @Override
    public D delUser(String id) {
        ArrayList<CustomersGoodes> oneUserAllGoodesInfo = shipmentRecordsMapper.getOneUserAllGoodesInfo(id);
        Customers id2 = getOne(new QueryWrapper<Customers>().eq("id", id));
        D build = D.builder().build();
        build.inputVal("用户信息", id2);
        build.inputVal("会删除的用户的物品信息（包含仓库信息）", oneUserAllGoodesInfo);

//        这里只要删除用户表的用户id就可以了，因为MySQL使用了一个外键作为连接删除掉用户表当中的id之后链接表（状态表）跟着更新数据
        int id1 = super.baseMapper.delete(new QueryWrapper<Customers>().eq("id", id));

        return build;
    }

    @Override
    public D addUser(Customers customers) {
        customers.setId(UUID.randomUUID().toString());
        boolean save = save(customers);
        D build = D.builder().build();
        if (!save) {
            build.inputVal("保存失败！", customers);
        } else {
            build.inputVal("保存成功！", customers);
        }
        return build;
    }
}
